#include"LVGL_APP.h"


#define MY_SYMBOL_OPEN "\xEE\x98\x95"
#define MY_SYMBOL_FG_LOCK   "\xEE\x9A\xB8"
#define MY_SYMBOL_PSWD_LOCK "\xEE\x98\x92"
#define MY_SYMBOL_NFC   "\xEE\xA6\x90"
#define MY_SYMBOL_LOCK "\xEE\x99\x8B"





#ifdef MY_PSWD_SYMBOL_40
#define MY_PSWD_SYMBOL MY_PSWD_SYMBOL1_40
#else
#define MY_PSWD_SYMBOL MY_PSWD_SYMBOL1_20
#endif

LV_FONT_DECLARE(MY_PSWD_SYMBOL);

static const char* APP_Map[] = { MY_SYMBOL_OPEN,"\n",MY_SYMBOL_FG_LOCK,MY_SYMBOL_PSWD_LOCK,MY_SYMBOL_NFC,""};
static const char* APP_text_Map[] = { "open","\n","fg ","pwd ","NFC",""};

lv_obj_t* Print_ta =NULL;




/*
返回按键的回调函数
*/
void Return_Btn_e_cb(lv_event_t* e)
{
    lv_obj_t* target = lv_event_get_target(e);
  
        if ((Return_Btn_Args_t*)e->user_data != NULL)
        {
            Return_Btn_Args_t P = *(Return_Btn_Args_t *)e->user_data;
            lv_obj_clear_flag(P.Prev_Page,LV_OBJ_FLAG_HIDDEN);//清除上一页的隐藏标志。
           if(P.ReBtn_CB!=NULL)
           {
            P.ReBtn_CB(P.User_Data); //调用回调函数
           }
           lv_free((Return_Btn_Args_t*)e->user_data);
        }
        lv_obj_del(lv_obj_get_parent(target));//删除基础页
}

lv_obj_t* Return_Btn_Create(lv_obj_t* Current_BasePage,Return_Btn_Args_t* Args)//callback函数用于处理内存问题
{
    lv_obj_t* Return_label = lv_label_create(Current_BasePage);
    lv_label_set_text(Return_label, LV_SYMBOL_CLOSE);
    lv_obj_add_flag(Return_label, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_text_color(Return_label, lv_color_hex(0xff0000), LV_STATE_PRESSED);
    lv_obj_add_event_cb(Return_label, Return_Btn_e_cb, LV_EVENT_CLICKED, Args);
    lv_obj_set_size(Return_label, lv_pct(10), lv_pct(10));
    lv_obj_set_align(Return_label, LV_ALIGN_TOP_RIGHT);

    return Return_label;
}

void TextArea_Create_e_cb(lv_event_t* e)
{
    uint8_t id;
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);
    const char* data = NULL;
    
    switch (code1)
    {
    case LV_EVENT_FOCUSED: //密码框聚集
    {

        // lv_keyboard_set_textarea(kb1, ta1);
        lv_keyboard_set_textarea((lv_obj_t*)e->user_data, temp1);
        lv_obj_clear_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    case LV_EVENT_DEFOCUSED:
    {

        lv_keyboard_set_textarea((lv_obj_t*)e->user_data, NULL);
        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
        break;
    }
    case LV_EVENT_VALUE_CHANGED://密码输入
    {

        id = lv_btnmatrix_get_selected_btn(temp1);
        const char* txt = lv_btnmatrix_get_btn_text(temp1, id);

        if (strcmp(txt, LV_SYMBOL_KEYBOARD) == 0)
        {
            if (lv_keyboard_get_mode(temp1) != LV_KEYBOARD_MODE_NUMBER)
            {

                lv_keyboard_set_mode(temp1, LV_KEYBOARD_MODE_NUMBER);
            }
            else
            {
                lv_keyboard_set_mode(temp1, LV_KEYBOARD_MODE_TEXT_LOWER);
            }
        }
        if (strcmp(txt, LV_SYMBOL_OK) == 0)
        {
            
                data = lv_textarea_get_text(((TextArea_Date_t*)e->user_data)->ta);//获取键盘的数据
                if (((TextArea_Date_t*)e->user_data)->User_Data != NULL)
                {

                    for (int i = 0; i < strlen(data); i++)
                    {
                        //  printf("txt[%d] = %x\n", i, data[i]);
                        *((TextArea_Date_t*)e->user_data)->User_Data++ = data[i];

                    }
                    
                }
                if (((TextArea_Date_t*)e->user_data)->Pri_Page != NULL)//恢复显示上上一页
                {
                    lv_obj_clear_flag(((TextArea_Date_t*)e->user_data)->Pri_Page, LV_OBJ_FLAG_HIDDEN);
                }

                lv_free((TextArea_Date_t*)e->user_data);
                if(lv_obj_get_parent(temp1) != lv_scr_act())//
                {
                lv_obj_del(lv_obj_get_parent(temp1));//获取文本框的基础页，并删除。
                }
        }
        break;
    }

    default:
        break;
    }

}
void TA_C_RrBtn_CallBack(void * p)
{
    lv_free((TextArea_Date_t*)p);
}
lv_obj_t * TextArea_Create(lv_obj_t* Prev_Page,uint32_t Max_Lebgth,const char * placeholder_tex,bool password_mode, char * User_Data)
{
    //创建基础页
    lv_obj_t* Base_Page = Base_Page_Create();
    
    Return_Btn_Args_t* Return_Btn_Args = (Return_Btn_Args_t*)lv_malloc(sizeof(Return_Btn_Args_t));
    TextArea_Date_t* taData = (TextArea_Date_t*)lv_malloc(sizeof(TextArea_Date_t));



    lv_obj_t* ta1 = lv_textarea_create(Base_Page);
    lv_obj_t* kb1 = lv_keyboard_create(Base_Page);
    lv_obj_set_size(ta1, lv_pct(50), lv_pct(40));
    lv_obj_set_size(kb1, lv_pct(100), lv_pct(50));

    lv_obj_align(ta1, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_align(kb1, LV_ALIGN_BOTTOM_MID, 0, 0);

    lv_textarea_set_password_mode(ta1, password_mode);
    if(placeholder_tex != NULL)
    {
    lv_textarea_set_placeholder_text(ta1, placeholder_tex);
    }
    lv_textarea_set_max_length(ta1, Max_Lebgth);
    taData->ta = ta1;
    taData->Pri_Page = Prev_Page;
    taData->User_Data = User_Data;
         /*将需要free 的数据传入，在event_cb 中调用callback 函数。*/
    Return_Btn_Args->Prev_Page = Prev_Page;
    Return_Btn_Args->ReBtn_CB = TA_C_RrBtn_CallBack;
    Return_Btn_Args->User_Data = taData;
    Return_Btn_Create(Base_Page,Return_Btn_Args);


    lv_obj_add_event_cb(kb1, TextArea_Create_e_cb, LV_EVENT_VALUE_CHANGED, taData);
  //  lv_keyboard_set_popovers(kb1, true);
    lv_obj_add_event_cb(ta1, TextArea_Create_e_cb, LV_EVENT_FOCUSED, kb1);
    lv_obj_add_event_cb(ta1, TextArea_Create_e_cb, LV_EVENT_DEFOCUSED, kb1);

    lv_obj_add_flag(kb1, LV_OBJ_FLAG_HIDDEN);
	return ta1;
}


lv_obj_t * Base_Page_Create(void)
{
    lv_obj_t* Base_Page = lv_obj_create(lv_scr_act());
    lv_obj_set_size(Base_Page, lv_pct(100), lv_pct(100));
    lv_obj_set_style_bg_color(Base_Page, lv_color_hex(0xFFFFFF), LV_STATE_DEFAULT);
    return Base_Page;
}

void LVGL_Show_Info_e_cb(lv_event_t* e)
{
    lv_obj_t* target = lv_event_get_target(e);
    lv_event_code_t code = lv_event_get_code(e);
    if (code == LV_EVENT_CLICKED)
    {
        
        if ((lv_obj_t*)e->user_data != NULL)
        {

            lv_obj_clear_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
        }
        Print_ta = NULL;
        lv_obj_del(lv_obj_get_parent(target));//删除打印界面
    }
   
    
}
void LVGL_Show_Info(const char* fmt,...)
{
   va_list ap;
   va_start(ap,fmt);
    if (Print_ta == NULL)
    {
      
        /*1 创建基础页*/
         lv_obj_t* Print_BasePage = Base_Page_Create();
 
        /*1 创建标签*/
        lv_obj_t* la1 = lv_label_create(Print_BasePage);
        lv_obj_add_flag(la1, LV_OBJ_FLAG_HIDDEN);
       
       lv_label_t* label = (lv_label_t*)la1;
       label->text = _lv_txt_set_text_vfmt(fmt, ap);

        char* txt1 = lv_label_get_text(la1);
        /*1 创建返回键*/

        lv_obj_t* Return_label = lv_label_create(Print_BasePage);

        lv_label_set_text(Return_label, LV_SYMBOL_CLOSE);
        lv_obj_add_flag(Return_label, LV_OBJ_FLAG_CLICKABLE);
        lv_obj_set_style_text_color(Return_label, lv_color_hex(0xff0000), LV_STATE_PRESSED);
        lv_obj_add_event_cb(Return_label, LVGL_Show_Info_e_cb, LV_EVENT_CLICKED,NULL );
        lv_obj_set_size(Return_label, lv_pct(10), lv_pct(10));
        lv_obj_set_align(Return_label, LV_ALIGN_TOP_RIGHT);



        /*1 创建文本框*/
        Print_ta = lv_textarea_create(Print_BasePage);
        lv_textarea_add_text(Print_ta, txt1);
        lv_obj_set_align(Print_ta, LV_ALIGN_CENTER);
        lv_obj_set_size(Print_ta, lv_pct(80), lv_pct(80));
    }
    else
    {
        lv_label_t* label = (lv_label_t*)lv_obj_get_child(lv_obj_get_parent(Print_ta), 0);
       label->text = _lv_txt_set_text_vfmt(fmt, ap);
      // lv_label_set_text_fmt(lv_obj_get_child(lv_obj_get_parent(Print_ta), 0), fmt, ap);
        char* txt1 = lv_label_get_text(lv_obj_get_child(lv_obj_get_parent(Print_ta), 0));
        lv_textarea_add_text(Print_ta, txt1);
    }

    va_end(ap);
   
}
void My_LVGL_Print(const char* fmt,...)
{
    va_list ap;
   va_start(ap,fmt);
   lv_obj_t * laBasePage = Base_Page_Create();

    lv_obj_t* la1 = lv_label_create(laBasePage);
    lv_obj_add_flag(la1, LV_OBJ_FLAG_HIDDEN);
   
    lv_label_t* label = (lv_label_t*)la1;
    label->text = _lv_txt_set_text_vfmt(fmt, ap);
 
    Return_Btn_Create(laBasePage,NULL);

    char * txt1 = lv_label_get_text(la1);
    const char* ss[] = { "Comtiues","close" ,""};
    lv_obj_t* LV_msgbox = lv_msgbox_create(laBasePage, "print info", txt1, ss, false);
    lv_obj_set_align(LV_msgbox,LV_ALIGN_TOP_MID);
    lv_obj_set_size(LV_msgbox,lv_pct(60),lv_pct(60));
    va_end(ap);
}
void My_LVGL_Scanf(const char* fmt,...)
{
    va_list ap;
    va_start(ap,fmt);

    va_end(ap);


}


void Desktop_e_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* target = lv_event_get_target(e);
    uint8_t ID = lv_btnmatrix_get_selected_btn(target);
    lv_obj_t* page = lv_obj_get_parent(target);
    lv_obj_add_flag(page,LV_OBJ_FLAG_HIDDEN);
    printf("%d\n", ID);
    switch (ID)
    {
    case 0: {
       RTOS_Pswd_Open((lv_obj_t*)e->user_data);
      //PWD_MATR((lv_obj_t*)e->user_data,NULL);//桌面按键基础页的地址-》功能函数-》回传到功能函的回调函数-》将桌面按键基础页恢复隐藏
        break;
    }
    case 1: {
		
		 // lv_obj_t * ta = TextArea_Create(NULL, lv_scr_act(), 3, "pls set pswd numb: 1 - 255", false, NULL);
      
     
        lv_obj_clear_flag(page,LV_OBJ_FLAG_HIDDEN);
        LVGL_Show_Info("abcd: %d\n ",ID);
        LVGL_Show_Info("abcd: \n ");
        LVGL_Show_Info("abcd: \n ");
        break;
		}
    case 2: {
       
       PSWD_Meun_Page((lv_obj_t*)e->user_data);

        break;
    }
    default: break;
    }
 
}
void Desktop(void)
{   
     /*创建桌面图层*/
    lv_obj_t* DesktopPage = lv_obj_create(lv_scr_act());
    lv_obj_set_size(DesktopPage, lv_pct(100), lv_pct(100));
    lv_obj_set_style_bg_color(DesktopPage, lv_color_hex(0xFFFFFF),LV_STATE_DEFAULT);
    /*----------------------------创建文本矩阵----------------------------------------------------*/
    lv_obj_t* APP_text_Matrix = lv_btnmatrix_create(DesktopPage);
    lv_btnmatrix_clear_btn_ctrl_all(APP_text_Matrix, LV_BTNMATRIX_CTRL_RECOLOR);
    lv_btnmatrix_set_map(APP_text_Matrix, APP_text_Map);
    lv_obj_align(APP_text_Matrix, LV_ALIGN_CENTER, 0, 40);
    lv_obj_set_size(APP_text_Matrix, lv_pct(80), lv_pct(80));

    lv_obj_set_style_pad_column(APP_text_Matrix, 20, LV_PART_MAIN);
    lv_obj_set_style_pad_row(APP_text_Matrix, 30, LV_PART_MAIN);
    
    lv_obj_set_style_border_width(APP_text_Matrix, 0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(APP_text_Matrix, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_opa(APP_text_Matrix, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(APP_text_Matrix, 0, LV_PART_ITEMS);
    lv_obj_set_style_border_opa(APP_text_Matrix, 0, LV_PART_ITEMS);
    lv_obj_set_style_bg_opa(APP_text_Matrix, 0, LV_PART_ITEMS);
    lv_obj_set_style_shadow_opa(APP_text_Matrix, 0, LV_PART_ITEMS);

    /*----------------------------创建图标矩阵----------------------------------------------------*/
    lv_obj_t* APP_Matrix = lv_btnmatrix_create(DesktopPage);
    lv_btnmatrix_clear_btn_ctrl_all(APP_Matrix, LV_BTNMATRIX_CTRL_RECOLOR);
    lv_btnmatrix_set_map(APP_Matrix, APP_Map);


    lv_obj_set_align(APP_Matrix, LV_ALIGN_CENTER);
    lv_obj_set_size(APP_Matrix, lv_pct(80), lv_pct(80));
    lv_obj_set_style_text_font(APP_Matrix,&MY_PSWD_SYMBOL,LV_PART_ITEMS);


    lv_obj_set_style_border_width(APP_Matrix, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(APP_Matrix, 1, LV_PART_ITEMS);
    lv_obj_set_style_border_opa(APP_Matrix,0, LV_PART_MAIN);
    lv_obj_set_style_border_opa(APP_Matrix,LV_OPA_30, LV_PART_ITEMS);
    lv_obj_set_style_bg_opa(APP_Matrix,0,LV_PART_MAIN);
    lv_obj_set_style_bg_opa(APP_Matrix, LV_OPA_10, LV_PART_ITEMS);

  
    lv_obj_set_style_pad_column(APP_Matrix, 20,LV_PART_MAIN);
    lv_obj_set_style_pad_row(APP_Matrix, 30, LV_PART_MAIN);
    lv_obj_set_style_text_color(APP_Matrix, lv_color_hex(0xff00), LV_PART_ITEMS);
    lv_obj_add_event_cb(APP_Matrix, Desktop_e_cb, LV_EVENT_VALUE_CHANGED, DesktopPage);
   
}


void PWD_MATR(lv_obj_t* PRI_Page)
{   
    
    /*创建基础页*/
  //  lv_obj_t* CH_PWD_Page = Base_Page_Create();
    
    /*创建返回按键*/
    // Return_Btn_Create(CH_PWD_Page, PRI_Page);
    /*创建密码输入*/
//    TextArea_Create(PRI_Page,CH_PWD_Page,16,"Password",true,NULL);
             /*-------------------FreeRTOS*-----------------*/


}
/*
密码菜单页面—事项函数
*/
void PWDMeun_Add_New_PWD_Btn_e_cb(lv_event_t* e)
{
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);

    if (code1 == LV_EVENT_CLICKED)//返回到桌面
    {
     
        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
        RTOS_PswdMeun_Add_New_PWD((lv_obj_t*)e->user_data);
  //      PWDMeun_Add_New_PWD((lv_obj_t*)e->user_data);//添加密码
    }
}
void PWDMeun_Chge_PWD_Btn_e_cb(lv_event_t* e)
{
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);

    if (code1 == LV_EVENT_CLICKED)//返回到桌面
    {
        
        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
    }
}
void PWDMeun_Review_PWD_Btn_e_cb(lv_event_t* e)
{
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);

    if (code1 == LV_EVENT_CLICKED)//返回到桌面
    {
        
        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
    }
}
void PWDMeun_Dele_PWD_Btn_e_cb(lv_event_t* e)
{
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);

    if (code1 == LV_EVENT_CLICKED)//返回到桌面
    {
        
        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
    }
}
/*
密码菜单页面—功能函数
*/
void PSWD_Meun_ReBtn_CallBack(void *p)
{

}
void PSWD_Meun_Page(lv_obj_t* PRI_Page)
{   /*创建基础页*/
    lv_obj_t* PWD_Menu_Page = Base_Page_Create();
   
    /*创建返回按键*/
    Return_Btn_Args_t* Return_Btn_Args = (Return_Btn_Args_t*)lv_malloc(sizeof(Return_Btn_Args_t));
    Return_Btn_Args->Prev_Page = PRI_Page;
    Return_Btn_Args->ReBtn_CB = NULL;
    Return_Btn_Args->User_Data = NULL;
    Return_Btn_Create(PWD_Menu_Page,Return_Btn_Args);
    /*创建密码菜单*/
    lv_obj_t* PWD_Menus_List = lv_list_create(PWD_Menu_Page);
    lv_obj_set_align(PWD_Menus_List, LV_ALIGN_CENTER);
    lv_obj_set_size(PWD_Menus_List, lv_pct(70), lv_pct(100));
    lv_list_add_text(PWD_Menus_List,"Pssword Meun");


    /**1.添加新密码**/
    lv_obj_t* PWDMeun_Add_New_PWD_Btn =lv_list_add_btn(PWD_Menus_List, NULL, "Add New Password");
    lv_obj_add_event_cb(PWDMeun_Add_New_PWD_Btn, PWDMeun_Add_New_PWD_Btn_e_cb, LV_EVENT_CLICKED, PWD_Menu_Page);//把基础页上传，在回调函数中隐藏基础页
    /**2.修改密码**/
    lv_obj_t* PWDMeun_Chge_PWD_Btn = lv_list_add_btn(PWD_Menus_List, NULL, "Change Password");
    lv_obj_add_event_cb(PWDMeun_Add_New_PWD_Btn, PWDMeun_Chge_PWD_Btn_e_cb, LV_EVENT_CLICKED, PWD_Menu_Page);
    /**3.查看密码**/
    lv_obj_t* PWDMeun_Review_PWD_Btn = lv_list_add_btn(PWD_Menus_List, NULL, "Review  Password");
    lv_obj_add_event_cb(PWDMeun_Add_New_PWD_Btn, PWDMeun_Review_PWD_Btn_e_cb, LV_EVENT_CLICKED, PWD_Menu_Page);
    /**4.删除密码**/
    lv_obj_t* PWDMeun_Dele_PWD_Btn = lv_list_add_btn(PWD_Menus_List, NULL, "Delete Password");
    lv_obj_add_event_cb(PWDMeun_Add_New_PWD_Btn, PWDMeun_Dele_PWD_Btn_e_cb, LV_EVENT_CLICKED, PWD_Menu_Page);
    
  
}
/*-----------添加新密码------------------------*/
void Add_New_PWD_Msg_event_cb(lv_event_t * e)
{
    lv_obj_t * btn = lv_event_get_current_target(e);

    if(lv_msgbox_get_active_btn(btn) == 0)
    {
       *(uint8_t *)e->user_data = 1;
    }else
    {
        *(uint8_t *)e->user_data = 2;
    }

   // lv_obj_clear_flag(lv_obj_get_child(lv_obj_get_parent(btn),0) ,LV_OBJ_FLAG_HIDDEN);

    //  lv_msgbox_close(btn);
    lv_obj_del(lv_obj_get_parent(btn));
}

lv_obj_t* Add_New_PWD_Msg_Create (uint8_t *PWD_LEVL )
{
    lv_obj_t* Base_Page = Base_Page_Create();
  

     /*创建选择密码优先级消息框*/
     static const char * msg_btn[]={"Admain","User",""};

    lv_obj_t* CHG_PWD_LEVL_MSG = lv_msgbox_create(Base_Page, "select passwoed level", "plese select passwoed level !", msg_btn, false);
    lv_obj_set_size(CHG_PWD_LEVL_MSG, lv_pct(60), lv_pct(60));
    lv_obj_set_align(CHG_PWD_LEVL_MSG,LV_ALIGN_TOP_MID);
    lv_obj_add_event_cb(CHG_PWD_LEVL_MSG,Add_New_PWD_Msg_event_cb,LV_EVENT_VALUE_CHANGED,PWD_LEVL);
    return CHG_PWD_LEVL_MSG;
}






void PWDMeun_Chge_PWD(lv_event_t* e)
{
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);

    if (code1 == LV_EVENT_CLICKED)//返回到桌面
    {

        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
    }
}
void PWDMeun_Review_PWD(lv_event_t* e)
{
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);

    if (code1 == LV_EVENT_CLICKED)//返回到桌面
    {

        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
    }
}
void PWDMeun_Dele_PWD(lv_event_t* e)
{
    lv_event_code_t code1 = lv_event_get_code(e);
    lv_obj_t* temp1 = lv_event_get_target(e);

    if (code1 == LV_EVENT_CLICKED)//返回到桌面
    {

        lv_obj_add_flag((lv_obj_t*)e->user_data, LV_OBJ_FLAG_HIDDEN);
    }
}


/*
密码菜单页面—功能函数_end
*/





